package com.txm.controller;


import common.model.Result;
import common.util.IUploadService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tangxinmao on 2017/7/5.
 */
@Controller
@RequestMapping("upload")
public class UploadController {
   /* @Autowired
    private IUploadService uploadService;*/

    @Value("#{configProperties['allowDomainName']}")
    private String allowDomainName;
    @Value("#{configProperties['imgRoot']}")
    private String imgRoot;

    @ResponseBody
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public Result upload(HttpServletRequest httpServletRequest, MultipartFile file) throws IOException {
     /*   Result result = uploadService.upload(httpServletRequest, file);*/
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public Result uploadFile(HttpServletRequest httpServletRequest, MultipartFile file, String servletPath) throws IOException {
        Result result = new Result();
        String serverName = httpServletRequest.getServerName();
        StringUtils.split(allowDomainName, ",");
        String[] allowDomainNames = StringUtils.split(allowDomainName, ",");
        List<String> allowDomainNameList = new ArrayList<String>();
        CollectionUtils.addAll(allowDomainNameList, allowDomainNames);
        if (!allowDomainNameList.contains(serverName)) {
            return result;
        }
        java.io.File destination = new java.io.File(imgRoot + servletPath + "/" + file.getOriginalFilename());
        FileUtils.copyInputStreamToFile(file.getInputStream(), destination);
        result.setResult(true);
        return result;
    }

    @RequestMapping(value = "/uploaddfs", method = {RequestMethod.POST, RequestMethod.GET})
    public void email(MultipartFile file, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws MyException, IOException, NoSuchAlgorithmException {
        ClientGlobal.init(new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath());
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageServer storageServer = null;
        StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
        String substring = file.getContentType().substring(file.getContentType().lastIndexOf("/") + 1);
        NameValuePair[] nameValuePairs = new NameValuePair[3];
        nameValuePairs[0]=new NameValuePair("fileName",file.getOriginalFilename());
        nameValuePairs[1]=new NameValuePair("fileExtName",file.getContentType().substring(file.getContentType().lastIndexOf("/")+1));
        nameValuePairs[2]=new NameValuePair("fileLength",String.valueOf(file.getSize()));
        String s = storageClient.upload_file1(file.getBytes(), substring,nameValuePairs );
        String group = s.split("/")[0];
        String filed = s.substring(group.length() + 1);
        byte[] bytes = storageClient.download_file(group, filed);
        trackerServer.close();
        FileUtils.copyInputStreamToFile(new ByteArrayInputStream(bytes),new File("D:/xxxxxx.jpeg"));
        IOUtils.write(bytes,httpServletResponse.getWriter());
        IOUtils.closeQuietly(httpServletResponse.getWriter());

;

    }
}
